package com.niwodai.credit.tool.pdf;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

import org.apache.log4j.Logger;

/**
 * PDF处理服务
 * @author windsnowstar
 *
 */
public class PdfHandleServer {
	private static Logger logger = Logger.getLogger(App.class);

	public static void main(String[] args2) throws IOException {
		logger.error("Waiting for client request....");
		PrintWriter pwtoclien = null;
		ServerSocket ss = new ServerSocket(5678);;
		Scanner inScanner = null;
		try {
			while(true){
				Socket socket = ss.accept();
				pwtoclien = new PrintWriter(socket.getOutputStream());
				pwtoclien.flush();
				inScanner = new Scanner(socket.getInputStream());
				while(inScanner.hasNextLine()){
					String str = inScanner.nextLine();
					String path = "";
					String target = "";
					if (str != null && str != "" && str.indexOf(",") != -1) {
						String[] args = str.split(",");
						if (args.length >= 1) {
							path = args[0];
							if (args.length > 1) {
								target = args[1];
							}
							long start = System.currentTimeMillis();
							App.getAreaText(path, target);
							long end = System.currentTimeMillis();
							logger.error(path + "handled ,costTime:" + (end - start));
						}
						target = "";
					}else{
						logger.error("bad param:" + str );
					}
					pwtoclien.println("recieved and handled complete");
					pwtoclien.flush();
				}
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			pwtoclien.close();
			inScanner.close();
			try {
				ss.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
